package com.bai.ruiji.Controller;

import com.bai.ruiji.Common.BaseContext;
import com.bai.ruiji.Common.R;
import com.bai.ruiji.Service.UserService;
import com.bai.ruiji.pojo.User;
import com.bai.ruiji.utils.SMSUtils;
import com.bai.ruiji.utils.ValidateCodeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author 27471
 * Time : 2022/10/14 18:01
 */
@RequestMapping("/user")
@RestController//返回JSON数据
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 手机短信验证码
     *
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request) {
        //获取手机号
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
            //生成随机的4位验证码
            //String code = ValidateCodeUtils.generateValidateCode(4).toString();
            //log.info(code);
            //调用aliyun的短信服务的Api
            // SMSUtils.sendMessage("签名","模板code",phone,code);
            //需要将生成的验证码保存到session中
            request.getSession().setAttribute("phone", phone);
            //session.setAttribute("phone",phone);
            //return R.success("手机验证码发送成功");
        }

        return R.success("手机验证码发送失败");
    }

    /**
     * 移动端用户登录
     *
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpServletRequest request) {
        //获取手机号
        String phone = map.get("phone").toString();
        //获取验证码
        // String code = map.get("code").toString();
        //从session中获取保存的验证码
        // Object codeInSession = session.getAttribute(phone);
        //进行验证码的比对(页面提交的验证码和session中的验证码一致则登录成功)
        // if (codeInSession!=null&&codeInSession.equals(code)){
        //判断手机号是否在表里(是否为新用户)
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, phone);
        User user = userService.getOne(queryWrapper);

        if (user == null) {
            //新用户自动注册
            User user1 = new User();
            user1.setPhone(phone);
            user1.setStatus(1);
            userService.save(user1);
            request.getSession().setAttribute("user", user1.getId());
            return R.success(user);
        } else {
            if (user.getPhone().equals(phone)) {
                request.getSession().setAttribute("user", user.getId());
                return R.success(user);
            }
            return R.error("登录失败");
        }
    }

    /**
     * 退出登录
     */
    @PostMapping("/loginout")
    private R<String> exit(HttpServletRequest request){
       request.getSession().removeAttribute("user");
       return R.success("退出成功");
    }

}

